//
//  PhotoView.m
//  CMJWeibo
//
//  Created by BH on 16/1/4.
//
//

#import "PhotoView.h"
#import "Photo.h"
#import "UIImage+MJ.h"
#import "UIView+MJ.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface PhotoView ()
{
    UIImageView *gifView;
}

@end

@implementation PhotoView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        self.contentMode = UIViewContentModeScaleAspectFill;
        self.clipsToBounds = YES;  // 超出UIImageView边界的内容, 都剪掉(不显示)
        
        gifView = [[UIImageView alloc] initWithImage:[UIImage imageWithName:@"timeline_image_gif"]];
        [self addSubview:gifView];
    }
    return self;
}

-(void)setPhoto:(Photo *)photo
{
    _photo = photo;
    
    //下载图片
    NSURL *url = [NSURL URLWithString:photo.thumbnail_pic];
    [self sd_setImageWithURL:url placeholderImage:[UIImage imageWithName:@"timeline_image_placeholder"]];
    
    //gifView的可见性
    if ([photo.thumbnail_pic.lowercaseString hasSuffix:@"gif"]) {
        gifView.hidden = NO;
    }else{
        gifView.hidden = YES;
    }
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    gifView.x = self.width - gifView.width;
    gifView.y = self.height -gifView.height;
}

@end
